package com.agileai.bpm.controller.emulation;

import java.util.ArrayList;
import java.util.List;

import com.agileai.bpm.bizmoduler.emulation.TaskListQuery;
import com.agileai.bpm.bizmoduler.emulation.WfEmulationManage;
import com.agileai.bpm.domain.running.ProcessInstance;
import com.agileai.bpm.service.running.RunningException;
import com.agileai.bpm.webservice.BPMService;
import com.agileai.bpm.webservice.BizAttribute;
import com.agileai.bpm.webservice.ProcessProfile;
import com.agileai.bpm.webservice.SubmitWorkItem;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.controller.core.QueryModelDetailHandler;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.ListUtil;

public class TaskApproveFrameHandler extends QueryModelDetailHandler{
	public TaskApproveFrameHandler(){
		super();
		this.serviceId = buildServiceId(TaskListQuery.class);
	}
	
	public ViewRenderer prepareDisplay(DataParam param){
		String wfpId = param.get("WFP_ID");
		String wfInstanceId = param.get("WFIP_ID");
		String activityCode = param.get("WFA_CODE");
		String bizId = param.get("WFIP_BUSINESS_ID");
		
		param.put("WFP_ID", wfpId);
		param.put("WFIP_ID", wfInstanceId);
		param.put("WFA_CODE", activityCode);
		param.put("WFIP_BUSINESS_ID", bizId);

		DataRow record = getService().findDetail(param);
		this.setAttributes(record);

		this.processPageAttributes(param);
		return new LocalRenderer(getPage());
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ViewRenderer doAddBizDataAction(DataParam param){
		this.setAttributes(param);
		List bizDataList = new ArrayList();
		int bizDataSize = param.getInt("bizDataSize");
		for (int i=0;i < bizDataSize;i++){
			String bizDataName = param.get("bizDataName_"+i);
			String bizDataCode = param.get("bizDataCode_"+i);
			String bizDataValue = param.get("bizDataValue_"+i);
			DataRow row = new DataRow();
			row.put("bizDataName", bizDataName);
			row.put("bizDataCode", bizDataCode);
			row.put("bizDataValue", bizDataValue);
			bizDataList.add(row);
		}
		bizDataList.add(new DataRow());
		setAttribute("bizDataList", bizDataList);
		return prepareDisplay(param);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ViewRenderer doDelBizDataAction(DataParam param){
		this.setAttributes(param);
		int bizDataIndex = param.getInt("bizDataIndex");
		List bizDataList = new ArrayList();
		int bizDataSize = param.getInt("bizDataSize");
		for (int i=0;i < bizDataSize;i++){
			if (i == bizDataIndex)continue;
			String bizDataName = param.get("bizDataName_"+i);
			String bizDataCode = param.get("bizDataCode_"+i);
			String bizDataValue = param.get("bizDataValue_"+i);
			DataRow row = new DataRow();
			row.put("bizDataName", bizDataName);
			row.put("bizDataCode", bizDataCode);
			row.put("bizDataValue", bizDataValue);
			bizDataList.add(row);
		}
		setAttribute("bizDataList", bizDataList);
		return prepareDisplay(param);
	}
	
	@SuppressWarnings("rawtypes")
	protected void processPageAttributes(DataParam param) {
		String wfpId = param.get("WFP_ID");
		String bizRecordId = param.get("WFIP_BUSINESS_ID");
		
		List bizDataList = (List)getAttribute("bizDataList");
		if (bizDataList == null){
			bizDataList = new ArrayList();
		}
		setAttribute("bizDataList",bizDataList);
		setAttribute("bizDataSize",bizDataList.size());
		
		String wfInstanceId = param.get("WFIP_ID");
		ProcessHelper processHelper = ProcessHelper.instance();
		BPMService bpmService = processHelper.getBPMService();
		ProcessProfile processProfile = bpmService.retrieveProcessProfile(wfInstanceId);
		if (ProcessInstance.Status.running.equals(processProfile.getStatus())){
			setAttribute("canSubmit", "true");
		}else{
			setAttribute("canSubmit", "false");
		}
		
		String businessDetailURL = "index?WfEmulationManageDetail&isEmbed=true&WFP_ID="+wfpId+"&WFIP_ID="+wfInstanceId+"&bizRecordId="+bizRecordId+"&WFA_CODE="+param.get("WFA_CODE");
		setAttribute("businessDetailURL", businessDetailURL);
	}
	
	public ViewRenderer doSubmitAction(DataParam param) throws RunningException{
		ProcessHelper processHelper = ProcessHelper.instance();
		String wfpId = param.get("WFP_ID");
		String wfInstanceId = param.get("WFIP_ID");
		String activityCode = param.get("WFA_CODE");
		String activityName = param.get("WFA_NAME");
		String wfeId = param.get("WFIP_BUSINESS_ID");
		
		SubmitWorkItem processWorkItem = processHelper.createSubmitWorkItem(wfpId, wfInstanceId, activityCode);
		List<BizAttribute> attributeList = processWorkItem.getAttributeList();
		
		int bizDataSize = param.getInt("bizDataSize");
		List<DataRow> bizDataList = new ArrayList<DataRow>();
		for (int i=0;i < bizDataSize;i++){
			String bizDataName = param.get("bizDataName_"+i);
			String bizDataCode = param.get("bizDataCode_"+i);
			String bizDataValue = param.get("bizDataValue_"+i);
			DataRow row = new DataRow();
			row.put("bizDataName", activityName + "——" + bizDataName);
			row.put("bizDataCode", bizDataCode);
			row.put("bizDataValue", bizDataValue);
			
			BizAttribute attribute = new BizAttribute();
			attribute.setCode(bizDataCode);
			attribute.setValue(bizDataValue);
			attributeList.add(attribute);
			bizDataList.add(row);
		}
		WfEmulationManage wfEmulationManage = (WfEmulationManage)this.lookupService(WfEmulationManage.class);
		
		DataParam queryParam = new DataParam();
		queryParam.put("WFE_ID",wfeId);
		DataRow dataRow = wfEmulationManage.getRecord(queryParam);
		if (dataRow != null){
			String bizDataXML = dataRow.getString("WFE_CONTENT");
			List<DataRow> records = ListUtil.toDataSet(bizDataXML);
			if (records != null){
				records.addAll(bizDataList);
			}
			String newBizDataXML = ListUtil.toXML(records);
			DataParam newParam = dataRow.toDataParam(true);
			newParam.put("WFE_CONTENT",newBizDataXML);
			wfEmulationManage.updateRecord(newParam);
		}
		
		BPMService bpmService = processHelper.getBPMService();
		User user = (User)getUser();
		processWorkItem.setUserCode(user.getUserCode());
		
		bpmService.submitProcess(processWorkItem);
		return new RedirectRenderer(getHandlerURL(WfEmulationTaskListQueryHandler.class));
	}
	
	public ViewRenderer doBackAction(DataParam param){
		return new RedirectRenderer(getHandlerURL(WfEmulationTaskListQueryHandler.class));
	}
	
	protected TaskListQuery getService() {
		return (TaskListQuery)this.lookupService(this.getServiceId());
	}
}
